package com.leonzhangxf.spel.reference;

import com.leonzhangxf.spel.reference.bean.Inventor;
import com.leonzhangxf.spel.reference.bean.Society;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * Methods
 *
 * @author leonzhangxf
 * @date 20190427
 */
public class Methods {

    public static void main(String[] args) {
        SpelParserConfiguration parserConfiguration =
            new SpelParserConfiguration(true, true);
        SpelExpressionParser parser = new SpelExpressionParser(parserConfiguration);

        // string literal, evaluates to "bc"
        String bc = parser.parseExpression("'abc'.substring(1, 3)").getValue(String.class);
        System.out.println(bc);

        // evaluates to true
        Society society = new Society();
        society.getMembers().add(new Inventor("Mihajlo Pupin", "China"));

        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setRootObject(society);

        Boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')")
            .getValue(context, Boolean.class);
        System.out.println(isMember);
    }
}
